//
//  ChooseView.m
//  FreeTime
//
//  Created by 王江华 on 16/9/8.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//
#import "ChooseView.h"

#import "Header.h"

#import "XFSegmentControl.h"



#define SHARE_HEIGHT 150.0f
#define ANIMATION_TIME 0.3f

@interface ChooseView()

@property(nonatomic,strong)UIWindow *overlayWindow;

@property(nonatomic,strong)UIView *bgView;

@end

@implementation ChooseView

-(void)dismiss{

    [self.contentView removeFromSuperview];
    
    [self.bgView removeFromSuperview];
   
    self.overlayWindow = nil;
}
-(instancetype)initWithFrame:(CGRect)frame{
    
    self=[super initWithFrame:frame];
    
    self.frame = [UIScreen mainScreen].bounds;
    
    self.backgroundColor = [UIColor clearColor];
    
    self.alpha = 1.0f;
    
    [self addSubview:self.bgView];
    
    [self.overlayWindow addSubview:self];
    
    [self.overlayWindow makeKeyAndVisible];
    
    self.bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self.bgView addGestureRecognizer:tapGesture];
    if (self) {
        if(!_contentView){
            _contentView=[[UIView alloc]init];
            _contentView.layer.cornerRadius=5;
            _contentView.backgroundColor=[UIColor whiteColor];
            [self addSubview:_contentView];
        }
        if (!_sexPropertyLabel) {
            _sexPropertyLabel=[[UILabel alloc]init];
            _sexPropertyLabel.text=@"性别";
            _sexPropertyLabel.font=[UIFont systemFontOfSize:14];
            [_contentView addSubview:_sexPropertyLabel];
        }
        if(!_segmentControl){
            _segmentControl=[[XFSegmentControl alloc]init];
            _segmentControl.titiles=@[@"不限",@"男生",@"女生"];
            _segmentControl.clipsToBounds=YES;
            
            [_contentView addSubview:_segmentControl];
        }
        if (!_agePropertyLabel) {
            _agePropertyLabel=[[UILabel alloc]init];
            _agePropertyLabel.text=@"年龄";
            _agePropertyLabel.font=[UIFont systemFontOfSize:14];
            [_contentView addSubview:_agePropertyLabel];
        }
        if(!_scopePropertyLabel){
            _scopePropertyLabel=[[UILabel alloc]init];
            _scopePropertyLabel.text=@"范围";
            _scopePropertyLabel.font=[UIFont systemFontOfSize:14];
           [_contentView addSubview:_scopePropertyLabel];
        }
        if (!_sexline) {
            _sexline=[[UIView alloc]init];
            _sexline.backgroundColor=[ImageUtil colorWithRgb:0xe6e6e6];
            [_contentView addSubview:_sexline];
        }
        if (!_ageline) {
            _ageline=[[UIView alloc]init];
            _ageline.backgroundColor=[ImageUtil colorWithRgb:0xe6e6e6];
            [_contentView addSubview:_ageline];
        }
        if (!_scopeline) {
            _scopeline=[[UIView alloc]init];
            _scopeline.backgroundColor=[ImageUtil colorWithRgb:0xe6e6e6];
            [_contentView addSubview:_scopeline];
        }
        if (!_onelineView) {
            
            _onelineView=[[UIView alloc]init];
            
            _onelineView.backgroundColor=[ImageUtil colorWithRgb:0xe6e6e6];
            
            [_contentView addSubview:_onelineView];
        }
        if (!_twolineView) {
            
            _twolineView=[[UIView alloc]init];
            _twolineView.backgroundColor=[ImageUtil colorWithRgb:0xe6e6e6];
            
            [_contentView addSubview:_twolineView];
        }
        
        if(!_leftAgeImg){
            _leftAgeImg=[[UIImageView alloc]init];
            _leftAgeImg.image=[UIImage imageNamed:@"naifen"];
            [_contentView addSubview:_leftAgeImg];
        }
        if (!_rightAgeImg) {
            _rightAgeImg=[[UIImageView alloc]init];
            _rightAgeImg.image=[UIImage imageNamed:@"shafa"];
            [_contentView addSubview:_rightAgeImg];
            
        }
        if (!_leftScopeImg) {
            _leftScopeImg=[[UIImageView alloc]init];
            _leftScopeImg.image=[UIImage imageNamed:@"zhifeiji"];
            [_contentView addSubview:_leftScopeImg];
        }
        if (!_rightScopeImg) {
            _rightScopeImg=[[UIImageView alloc]init];
            _rightScopeImg.image=[UIImage imageNamed:@"feiji"];
            [_contentView addSubview:_rightScopeImg];
        }
        if(!_slider){
            _slider = [[UISlider alloc] init];
            _slider.minimumTrackTintColor = RGB_COLOR(255, 172, 166);
            _slider.maximumTrackTintColor = MWMAINCOLOR;
            [_slider addTarget:self action:@selector(sliderChanged) forControlEvents:UIControlEventValueChanged];
            [_slider setThumbImage:[UIImage imageNamed:@"Oval"] forState:UIControlStateNormal];
            [_slider setThumbImage:[UIImage imageNamed:@"Oval"] forState:UIControlStateHighlighted];
            [_contentView addSubview:_slider];
        }
        if(!_ageslider){
            _ageslider=[[XFCustomSlider alloc]initWithFrame:CGRectMake(15, 140, WindowWidth-140, 24)];
            _ageslider.valueMin = 0;
            _ageslider.valueMax = 99;
            _ageslider.valueMinCurrent =25;
            _ageslider.valueMaxCurrent =52;
            _ageslider.delegate = self;
            [_contentView addSubview:_ageslider];
        }
        if (!_ageLabel) {
            _ageLabel=[[UILabel alloc]init];
            _ageLabel.font=[UIFont systemFontOfSize:14];
            _ageLabel.textColor=[UIColor lightGrayColor];
            [_contentView addSubview:_ageLabel];
        }
        if (!_scopeLabel) {
            _scopeLabel=[[UILabel alloc]init];
            _scopeLabel.font=[UIFont systemFontOfSize:14];
            _scopeLabel.textColor=[UIColor lightGrayColor];
            [_contentView addSubview:_scopeLabel];
        }
        
        if (!_confirmBtn) {
            _confirmBtn=[[UIButton alloc]init];
            [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
            [_confirmBtn setTitleColor:darkRed forState:UIControlStateNormal];
            _confirmBtn.titleLabel.font=[UIFont systemFontOfSize:14];
            [_confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:1<<6];
             [_contentView addSubview:_confirmBtn];
        }
        if (!_btnlineView) {
            
            _btnlineView=[[UIView alloc]init];
            
            _btnlineView.backgroundColor=[ImageUtil colorWithRgb:0xe6e6e6];
            
            [_contentView addSubview:_btnlineView];
        }
        if (!_cancelBtn) {
            _cancelBtn=[[UIButton alloc]init];
            [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
            [_cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            
            _cancelBtn.titleLabel.font=[UIFont systemFontOfSize:14];
            
            [_cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:1<<6];
            
            [_contentView addSubview:_cancelBtn];
        }
    }
    return self;
}
-(void)sliderChanged{

    
}
-(UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.7;
        _bgView.userInteractionEnabled = YES;
    }
    return _bgView;
}
-(void)confirmAction:(UIButton*)sender{

    [self dismiss];
    
    if (self.confirmBlock) {
        
        self.confirmBlock();
    }

}
-(void)customSliderValueChanged:(id)sender{
    
    
    XFCustomSlider *slider = (XFCustomSlider *)sender;
  
    
}

-(void)cancelAction:(UIButton*)sender{


     [self dismiss];
    
    if (self.cancelBlcok) {
    
        self.cancelBlcok();
    }
}
-(void)layoutSubviews{

    [super layoutSubviews];
    
    _contentView.frame=CGRectMake(40, 10, WindowWidth-80, 255);
    
    _contentView.centerY=self.centerY;
    
    _sexPropertyLabel.frame=CGRectMake(15, 15, 120, 15);
    
    _sexline.frame=CGRectMake(10, CGRectGetMaxY(_sexPropertyLabel.frame)+5, 30, 1);
    
    
    _segmentControl.frame=CGRectMake(10, CGRectGetMaxY(_sexline.frame)+10, self.contentView.frame.size.width-20, 30);
    
    _onelineView.frame=CGRectMake(10, CGRectGetMaxY(_segmentControl.frame)+10, self.contentView.frame.size.width-20, 1);
    
    
    _agePropertyLabel.frame=CGRectMake(15, CGRectGetMaxY(_onelineView.frame)+10, 120, 15);
    
    _ageline.frame=CGRectMake(10, CGRectGetMaxY(_agePropertyLabel.frame)+5, 30, 1);
    
    
    _leftAgeImg.frame=CGRectMake(10, CGRectGetMaxY(_ageline.frame)+5, 15, 15);
    
    
    _rightAgeImg.frame=CGRectMake(self.contentView.frame.size.width-10-15, CGRectGetMaxY(_ageline.frame)+5, 15, 15);
    
    _ageslider.frame=CGRectMake(CGRectGetMaxX(_leftAgeImg.frame)+5, CGRectGetMaxY(_ageline.frame)+5, self.contentView.frame.size.width-CGRectGetMaxX(_leftAgeImg.frame)-5-30, 24);
    
    
    _scopePropertyLabel.frame=CGRectMake(15, CGRectGetMaxY(_leftAgeImg.frame)+5, 120, 15);
    
    _scopeline.frame=CGRectMake(15, CGRectGetMaxY(_scopePropertyLabel.frame)+5, 30, 1);
    
    
    _leftScopeImg.frame=CGRectMake(10, CGRectGetMaxY(_scopeline.frame)+5, 15, 15);
    
    _slider.frame=CGRectMake(CGRectGetMaxX(_leftScopeImg.frame)+5, CGRectGetMaxY(_scopeline.frame)+5, self.contentView.frame.size.width-CGRectGetMaxX(_leftScopeImg.frame)-5-30, 20);
    
    _rightScopeImg.frame=CGRectMake(self.contentView.frame.size.width-10-15,  CGRectGetMaxY(_scopeline.frame)+5, 15, 15);
    
    _twolineView.frame=CGRectMake(10, CGRectGetMaxY(_rightScopeImg.frame)+30, self.contentView.frame.size.width-20, 1);
    
    _confirmBtn.frame=CGRectMake(0, CGRectGetMaxY(_twolineView.frame)+1, self.contentView.frame.size.width/2, 35);
    
    _cancelBtn.frame=CGRectMake(self.contentView.frame.size.width/2, CGRectGetMaxY(_twolineView.frame)+1, self.contentView.frame.size.width/2, 35);
    
    _btnlineView.frame=CGRectMake(self.contentView.frame.size.width/2, CGRectGetMaxY(_twolineView.frame)+10, 1, 15);
}
-(UIWindow *)overlayWindow
{
    if (!_overlayWindow) {
        _overlayWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _overlayWindow.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _overlayWindow.backgroundColor = [UIColor clearColor];
        _overlayWindow.windowLevel = UIWindowLevelAlert;
        _overlayWindow.userInteractionEnabled = YES;
    }
    return _overlayWindow;
}
@end
